package com.quanyan.stadium.aop;

import com.quanyan.stadium.api.req.ReqCreateArrangeLiveGameEvent;
import com.quanyan.stadium.api.req.gameEventConfig.ReqClubCreateGameEvent;
import com.quanyan.stadium.api.req.gameEventConfig.ReqPersonCreateGameEvent;
import com.quanyan.stadium.entity.TbGameEvent;
import com.quanyan.stadium.entity.vo.GameEvent;
import com.quanyan.stadium.service.commonComponent.GameMQProduct;
import com.quanyan.stadium.service.commonComponent.OfficialGameCache;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.InvocationTargetException;

/**
 * Created by xiaoxiao on 16/9/14.
 *
 * 赛事创建的时候利用aop逻辑来拆分业务
 */
@Aspect
@Configuration
public class GameCreateAspect {


    private static final Logger logger = LoggerFactory.getLogger(GameCreateAspect.class);

    @Autowired
    private GameMQProduct gameMQProduct;

    @Autowired
    private OfficialGameCache officialGameCache;

    /**
     *
     * 监控俱乐部赛事的创建(对createGameEvent方法进行切面)
     * */
    @Pointcut("execution(* com.quanyan.stadium.service.gameEvent.impl.*.createGameEvent(..))")
    public void createGameEvent() {}

    /**
     *  俱乐部赛事创建完成,触发相应的操作
     *
     *  操作1:给用户中心发送消息
     *  操作2:给俱乐部中心发送消息
     *  操作3:缓存赛事基本信息到缓存
     *
     * */
    @AfterReturning(value="createGameEvent()", returning = "rtv")
    public void doAfter(JoinPoint jp, Object rtv) throws Throwable{
        GameEvent gameEvent = (GameEvent) rtv;
        TbGameEvent tbGameEvent = new TbGameEvent();
        tbGameEvent.setId(gameEvent.getId());
        ReqClubCreateGameEvent req = new ReqClubCreateGameEvent();
        int uid = 0;
        boolean isPersonPublish = false, isInternal = false;
        try{
            Object[] args= jp.getArgs();
            for (Object o:args) {
                if (o instanceof ReqClubCreateGameEvent) {
                    req = (ReqClubCreateGameEvent) o;
                }
                if (o instanceof Integer) {
                    uid = ((Integer) o).intValue();
                }
                if (o instanceof ReqPersonCreateGameEvent) {
                    isPersonPublish = true;
                }
                if (o instanceof ReqCreateArrangeLiveGameEvent) {
                    isInternal = true;
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        gameMQProduct.clubCreateGameToUserCenterMQ(tbGameEvent, uid);
        if (!isPersonPublish && !isInternal) {
            gameMQProduct.clubCreateGameToClubMQ(tbGameEvent, uid, req.getClubId());
        }
        try {
            officialGameCache.getGameEvent(gameEvent.getId());
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        logger.info("俱乐部创建赛事成功,开始缓存赛事信息.赛事ID{}", gameEvent.getId());
    }

}
